package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.service.UserService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "C端菜单浏览接口")
@RequestMapping("/user/dish")
@AllArgsConstructor
@Slf4j
public class UserDishController {

    private DishService dishService;
    private CategoryService categoryService;
    private DishFlavorMapper dishFlavorMapper;


    /**
     * 通过分类id查找菜品相关数据
     * @param
     * @return
     */
    @ApiOperation("通过分类id查找菜品相关数据")
    @GetMapping("/list")
    @Cacheable(cacheNames = "dish_cache", key = "'dish'+#categoryId")
    public Result<List<DishVO>> getDishById(@RequestParam Long categoryId){
        // 菜品信息
        List<Dish> dishs = dishService.listByActiveCategoryId(categoryId);

        // 封装菜品详细信息
        List<DishVO> dishsvo = dishs.stream().map(item -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(item,dishVO);
            // 查询分类名称
            String categoryName = categoryService.queryById(dishVO.getCategoryId()).getName();
            dishVO.setCategoryName(categoryName);

            // 查询风味
            List<DishFlavor> dishFlavors = dishFlavorMapper.queryByDishId(dishVO.getId());
            dishVO.setFlavors(dishFlavors);
            return dishVO;
        }).collect(Collectors.toList());

        log.info("菜品详细数据为:{}",dishsvo);
        return Result.success(dishsvo);
    }



}
